package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.SpecialDAO;
import com.diankangwo.domain.Special;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 专题控制器
 */
@Controller
@Api(description = "专题")
public class SpecialController {

    @Autowired
    private SpecialDAO specialDAO;

    /**
     * 获取专题列表
     *
     * @return
     */
    @ApiOperation(value = " 获取横幅列表", notes = "")
    @ResponseBody
    @RequestMapping(value = "/special/list", method = RequestMethod.GET)
    public String getList() {
        List<Special> specialsList = specialDAO.findAll();
        for (int i = 0; i < specialsList.size(); i++) {
            if (!specialsList.get(i).getEnabled()) {
                specialsList.remove(specialsList.get(i));
            }
        }
        String code = "2";
        String msg = "处理异常！";
        if (specialsList != null) {
            code = "1";
            msg = "查询成功！";
        } else {
            msg = "未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, specialsList);
    }

    /**
     * 通过ID获取专题信息
     *
     * @return
     */
    @ApiOperation(value = " 通过ID获取专题信息", notes = "")
    @ResponseBody
    @RequestMapping(value = "/special/getinfo", method = RequestMethod.GET)
    public String getInfo(Integer id) {
        Special specials = new Special();
        specials = specialDAO.find(id);
        String code = "2";
        String msg = "处理异常！";
        if (id != null) {
            if (specials != null) {
                code = "1";
                msg = "查询成功！";
            } else {
                msg = "未查询到用户信息！";
            }
        } else {
            msg = "参数不正确！";
        }
        return resultMsg.mapToJson(code, msg, specials);
    }
}
